import 'package:flutter/material.dart';

import 'grid_item_widget.dart';
import 'grid_model.dart';

void main() {
  runApp(MaterialApp(
    title: 'Flutter Demo',
    debugShowCheckedModeBanner: false,
    theme: ThemeData(
      primarySwatch: Colors.blue,
    ),
    home: HomePage(),
  ));
}

class HomePage extends StatefulWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List<GridModel> list = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    for (int i = 0; i < 10; i++) {
      list.add(new GridModel("grid${i + 1}", false));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("grid"),
        actions: [
          ElevatedButton(
            onPressed: () {
              List<String> selectList = [];
              list.forEach((element) {
                if (element.selected) {
                  selectList.add(element.title);
                }
              });
              print(selectList.toString());
            },
            child: Text("提交"),
          )
        ],
      ),
      body: GridView.builder(
        itemCount: list.length,
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          mainAxisSpacing: 12,
          crossAxisSpacing: 12,
        ),
        itemBuilder: (BuildContext context, int index) {
          GridModel item = list[index];
          return GridItemWidget(item);
        },
      ),
    );
  }
}
